package com.gitee.l0km.codegen.decorator.plugin;

import java.util.List;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;

import com.gitee.l0km.codegen.base.CodeGenUtils;
import com.gitee.l0km.codegen.base.plugin.BasePlugin;
import com.gitee.l0km.codegen.decorator.DecoratorConfiguration;
import com.gitee.l0km.codegen.decorator.EnumSupportConfiguration;
import com.gitee.l0km.codegen.decorator.EnumSupportConstants;
import com.gitee.l0km.codegen.decorator.EnumSupportGenerator;

/**
 * {@link EnumSupportGenerator}生成器运行插件,生成java 枚举类型 对应的支持类
 * @author guyadong
 *
 */
@Mojo(name = "enumsupport",defaultPhase=LifecyclePhase.COMPILE,requiresDependencyResolution=ResolutionScope.COMPILE)
public class EnumSupportPlugin extends BasePlugin implements EnumSupportConstants{
	/**
	 * enum class name
	 */
	@Parameter(required = true)
	private String baseClass = null;
	/**
	 * The target package name
	 */
	@Parameter(alias="package",required =true)
	private String pkg ;
	/**
	 * path list of source separated by semicolon
	 */
	@Parameter()
	private String sourcePath;

	/**
	 * Specifies the paths where javadoc will look for referenced classes (.class files),
	 * Ignored since 3.3.0
	 */
	@Parameter()
	private String classPath;
	/**
	 * comma-separated enum class name which generate interface classes of ${classname}Constant,<br>
	 * a simple example:  'mycompany.ErrorType', generate class ErrorTypeConstant.java,<br>
	 * another: 'mycompany.ErrorType:org.ErrorType/org.ErrorType',<br>
	 * the second example special slash-separated extends interface class names for generated constant interface class
	 */
	@Parameter()
	private String constantClasses;
	/**
	 * if true,add annotation to generated class,
	 * add @EnumSupportType for enum support class,@EnumConstantType for enum constant class
	 */
	@Parameter(defaultValue="false")
	private boolean withAnnotation = false;
	public EnumSupportPlugin() {
	}

	@Override
	protected String getTemplateFolder() {
		return EnumSupportConfiguration.DECORATOR_FOLDER;
	}

	@Override
	public void execute() throws MojoExecutionException, MojoFailureException {
		try {
			EnumSupportGenerator.main(makeArgs().toArray(new String[0]));
		} catch (Exception e) {
			throw new MojoExecutionException(e.getMessage(),e);
		}
	}

	@Override
	protected List<String> makeArgs() {
		List<String> args = super.makeArgs();
		args.add("--" + DecoratorConfiguration.PACKAGE_OPTION_LONG);
		args.add(pkg);
		if(null != baseClass){
			args.add("--" + DecoratorConfiguration.BASE_CLASS_OPTION_LONG);
			args.add(baseClass);
		}
		if(null != sourcePath){
			args.add("--" + DecoratorConfiguration.SOURCE_PREFIX_OPTION_LONG);
			args.add(sourcePath);
		}
		classPath = buildClasspath();
		args.add("--" + DecoratorConfiguration.CLASS_PATH_OPTION_LONG);
		args.add(classPath);
		/** 初始项目ClassLoader */
		CodeGenUtils.initProjectClassLoader(classPath, getClass().getClassLoader());
		if(null != constantClasses){
			args.add("--" + WITH_CONSTANT_OPTION_LONG);
			args.add(constantClasses);
		}
		if(withAnnotation){
			args.add("--" + WITH_ANNOT_OPTION_LONG);
		}
		return args;
	}

}
